<template>
  <a-drawer
    v-model:visible="vdata.visible"
    width="40%"
    :mask-closable="false"
    @close="close"
  >
    <template #title>
      <div
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
      >
        <span>数据解密</span>
        <!-- <a-button type="link" @click="exportCode(vdata.record)">导出渠道</a-button> -->
      </div>
    </template>
    <div>
      <a-row>
        <a-col span="24">
          <a-textarea v-model:value="vdata.data" :rows="5" placeholder="请输入要解密的数据" />
        </a-col>
        <a-col style="padding: 10px 0;">
          <a-button type="primary" @click="decrypt">解密</a-button>
        </a-col>
        <a-col :span="24">
          <!-- <a-textarea v-model:value="vdata.dataDecrypt" :rows="7" placeholder="解密后的数据"/> -->
          <!-- <json-viewer
          :value="vdata.dataDecrypt"
          :expand-depth="5"
          copyable
          boxed
          sort
          ></json-viewer> -->
          <pre ref="text" style="width: 100%;height: 100%;padding:5px;box-sizing: border-box; border: 1px solid gray;max-width: 100%;" />
        </a-col>
      </a-row>
    </div>
  </a-drawer>
</template>
<script setup>
    import { reactive,ref} from 'vue'
    import { sm4DecryptByResData, } from '@/utils/encryptUtil'
    const props = defineProps(['callBackFunc'])
    const vdata = reactive({
        visible:false,
        data:'',//要解密的数据
        dataDecrypt:'',//解密后的数据
    })
    const text = ref(null)

//     function checkObjectString(value) {
//   return String(value) === '[object Object]';
// }

function isJSON(str) {
  try {
    JSON.parse(str)
    return true
  } catch (e) {
    return false
  }
}

    // 解密
    function decrypt(){
    console.log(sm4DecryptByResData(vdata.data))
    let jsonText = sm4DecryptByResData(vdata.data)
    let formatDate = null
    console.log(11,jsonText)
    if(isJSON(jsonText)){
      console.log('走到了这里')
      formatDate = JSON.parse(sm4DecryptByResData(vdata.data))
    }else{
      formatDate =  sm4DecryptByResData(vdata.data)
    }
    text.value.innerHTML = JSON.stringify(formatDate, null, 4)
    }
    const close = () =>{
        vdata.visible = false
    }
    /**
 * 展开
 */
async function show(record) {
  vdata.visible = true
}

defineExpose({
  show,
})
</script>
<style lang="less">


</style>